<?php

class Account_AuthController extends Zend_Controller_Action
{
	public function loginAction()
	{
		if (Account_Model_Session::hasValidSession()) {
			$this->_redirect('/');
		}


		$flashMessages = array();

		$loginClosed = TRUE;
		$activationMethod = 'admin';

		$opts = Kernel::getInstance()->getOptions();
		if (is_array($opts) && is_array($opts['account'])) {
			$accOpts = $opts['account'];
			if (is_array($accOpts['login']) && isset($accOpts['login']['open'])
				&& intval($accOpts['login']['open']) === 1) {
				$loginClosed = FALSE;
			}
			if (is_array($accOpts['activation']) && isset($accOpts['activation']['method'])) {
				$activationMethod = $accOpts['activation']['method'];
			}
		}

		if ($loginClosed === TRUE) {
			$this->render('closed');
			return;
		}


		$request = $this->getRequest();
		$form    = new Account_Form_Login();

		// Check to see if this action has been POST'ed to.
		if ($this->getRequest()->isPost()) {

			// Now check to see if the form submitted exists, and
			// if the values passed in are valid for this form.
			if ($form->isValid($request->getPost())) {

				$values = $form->getValues();

				$accAuth = new Account_Model_Auth();

				$accountId = $accAuth->authenticate($values['identity'], $values['password']);

				Zend_Session::regenerateId();

				if (intval($accountId) > 0) {

					Account_Model_Session::beginSession($accountId);

					Euso_Model_Log::push('account', 'login.succeeded', NULL,
						array('identity' => $values['identity']));

					// Authentication succeeded
					$returnUri = $values['auth_success_return'];

					if (empty($returnUri) === FALSE) {
						$redirector = $this->_helper->getHelper('Redirector');
						$redirector->setPrependBase(FALSE)->gotoUrl($returnUri);
				//	} else if (isset($opts['session']['entry'])) {
				//		$this->_redirect($opts['session']['entry']);
					} else {
						$this->_redirect('/home');
					}

				} else {

					Euso_Model_Log::push('account', 'login.failed', NULL,
						array('identity' => $values['identity']));

					if ($accAuth->getErrorCode() === Account_Model_Auth::ACCOUNT_INACTIVE) {
						if ($activationMethod === 'email') {
							$flashMessages[] = "Account is not activated yet. Please check your email for your account activation procedure.";
						} else {
							$flashMessages[] = "Your account is waiting activation from the administrator.";
						}
					} else {
						$flashMessages[] = "Login failed. Please check your username and password.";
					}

				}
			}
		} else {
			$form->auth_success_return->setValue($request->getParam('return'));
		}

		if ($this->_getParam('fmsg')) {
			$flashMessenger = $this->_helper->FlashMessenger;
			$flashMessenger->setNamespace($this->_getParam('fmsg'));
			$this->view->flashMessages = $flashMessenger->getMessages();
		}
		if (is_array($flashMessages)) {
			if (is_array($this->view->flashMessages)) {
				$this->view->flashMessages = array_unique(array_merge($this->view->flashMessages, $flashMessages));
			} else {
				$this->view->flashMessages = $flashMessages;
			}
		}


		// Assign the form to the view
		$this->view->loginForm = $form;


		Kernel::getInstance()->setHeadTitle('Login');

		$this->view->singleWidgetPageTitle = 'Login';
		$this->view->singleWidgetPage = TRUE;
	}

	public function logoutAction()
	{
		$sessAcc = Account_Model_Session::getAccount();

		if ($sessAcc === NULL) {
			Euso_Model_Log::push('account', 'logout');
		} else {
			Euso_Model_Log::push('account', 'logout', NULL,
				array('id' => $sessAcc->getId(), 'idname' => $sessAcc->getIdName()));
		}

		Account_Model_Session::endSession();

		// Redirect to front page
		$this->_redirect('/');
	}
}
